package com.hongye.interceptor;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.log4j.MDC;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;

/**
 * 用户认证拦截
 * @author changpf
 *
 */
public class AuthInterceptor implements Interceptor {
	private Logger log=Logger.getLogger(getClass());
	public AuthInterceptor() {
	}

	public void intercept(Invocation inv) {
		Controller con=inv.getController();
//		登录检查
		if(con.getSession().getAttribute("userid")==null){
//			con.renderJsp("index.jsp");
			MDC.put("userid", "guest");
			if (log.isInfoEnabled()) {//记录登录日志信息
//				log.info(Utils.getClientInfo(con.getRequest()));
			}
			try {
				con.getResponse().sendRedirect("./../index.jsp");
			} catch (IOException e) {
				e.printStackTrace();
			};
			return ;
		}
		inv.invoke();
		MDC.put("userid",con.getSession().getAttribute("userid"));
	}

}
